import 'package:flutter/material.dart';
import 'package:flutter_app/constants.dart' show Constants, AppColors;
import 'conversation_page.dart';
import 'contact_page.dart';
import 'discover_page.dart';
import 'my_page.dart';

class HomeScreen extends StatefulWidget {
  final Widget child;

  HomeScreen({Key key, this.child}) : super(key: key);

  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  PageController _pageController;

  List<Widget> _pages;

  int _currentIndex = 0;

  @override
  void initState() {
    super.initState();
    _pageController = PageController(initialPage: _currentIndex);
    _pages = [
      ConversationPage(),
      ContactPage(),
      DiscoverPage(),
      MyPage()
    ];
  }

  @override
  Widget build(BuildContext context) {
    final BottomNavigationBar botNavBar = BottomNavigationBar(
      fixedColor: Colors.green,
      items: [
        BottomNavigationBarItem(
            icon: Icon(IconData(0xe608, fontFamily: Constants.IconFontFamily)),
            activeIcon:
                Icon(IconData(0xe603, fontFamily: Constants.IconFontFamily)),
            title: Text("微信")),
        BottomNavigationBarItem(
            icon: Icon(IconData(0xe601, fontFamily: Constants.IconFontFamily)),
            activeIcon:
                Icon(IconData(0xe656, fontFamily: Constants.IconFontFamily)),
            title: Text("通讯录")),
        BottomNavigationBarItem(
            icon: Icon(IconData(0xe600, fontFamily: Constants.IconFontFamily)),
            activeIcon:
                Icon(IconData(0xe671, fontFamily: Constants.IconFontFamily)),
            title: Text("发现")),
        BottomNavigationBarItem(
            icon: Icon(IconData(0xe6c0, fontFamily: Constants.IconFontFamily)),
            activeIcon:
                Icon(IconData(0xe626, fontFamily: Constants.IconFontFamily)),
            title: Text("我"))
      ],
      currentIndex: _currentIndex,
      type: BottomNavigationBarType.fixed,
      onTap: (index) {
        setState(() {
          _currentIndex = index;
          _pageController.animateToPage(_currentIndex,
              duration: Duration(microseconds: 100), curve: Curves.easeInOut);
        });
      },
    );

    return Scaffold(
        appBar: AppBar(
          title: Text("微信"),
          elevation: 0.0,
          actions: <Widget>[
            IconButton(
                icon: Icon(Icons.search),
                onPressed: () {
                  print("search");
                }),
            PopupMenuButton(
              icon: Icon(Icons.add),
              itemBuilder: (BuildContext context) {
                return <PopupMenuItem<String>>[
                  PopupMenuItem(
                    child: Row(
                      children: <Widget>[
                        Icon(
                          IconData(0xe69e,
                              fontFamily: Constants.IconFontFamily),
                          color: Color(AppColors.AppBarPopupMenuColor),
                        ),
                        Container(
                          width: 20.0,
                        ),
                        Text(
                          "发起群聊",
                          style: TextStyle(
                              color: Color(AppColors.AppBarPopupMenuColor)),
                        ),
                      ],
                    ),
                    value: "GROUP_CHAT",
                  ),
                  PopupMenuItem(
                    child: Row(
                      children: <Widget>[
                        Icon(
                          IconData(0xe638,
                              fontFamily: Constants.IconFontFamily),
                          color: Color(AppColors.AppBarPopupMenuColor),
                        ),
                        Container(
                          width: 20.0,
                        ),
                        Text(
                          "添加朋友",
                          style: TextStyle(
                              color: Color(AppColors.AppBarPopupMenuColor)),
                        ),
                      ],
                    ),
                    value: "ADD_FRIEND",
                  ),
                  PopupMenuItem(
                    child: Row(
                      children: <Widget>[
                        Icon(
                          IconData(0xe61b,
                              fontFamily: Constants.IconFontFamily),
                          color: Color(AppColors.AppBarPopupMenuColor),
                        ),
                        Container(
                          width: 20.0,
                        ),
                        Text(
                          "扫一扫",
                          style: TextStyle(
                              color: Color(AppColors.AppBarPopupMenuColor)),
                        ),
                      ],
                    ),
                    value: "QR_SCAN",
                  ),
                  PopupMenuItem(
                    child: Row(
                      children: <Widget>[
                        Icon(
                          IconData(0xe62a,
                              fontFamily: Constants.IconFontFamily),
                          color: Color(AppColors.AppBarPopupMenuColor),
                        ),
                        Container(
                          width: 20.0,
                        ),
                        Text(
                          "收付款",
                          style: TextStyle(
                              color: Color(AppColors.AppBarPopupMenuColor)),
                        ),
                      ],
                    ),
                    value: "PAYMENT",
                  ),
                  PopupMenuItem(
                    child: Row(
                      children: <Widget>[
                        Icon(
                          IconData(0xe63d,
                              fontFamily: Constants.IconFontFamily),
                          color: Color(AppColors.AppBarPopupMenuColor),
                        ),
                        Container(
                          width: 20.0,
                        ),
                        Text(
                          "帮助与反馈",
                          style: TextStyle(
                              color: Color(AppColors.AppBarPopupMenuColor)),
                        ),
                      ],
                    ),
                    value: "HELP",
                  ),
                ];
              },
              onSelected: (selected) {
                print("$selected");
              },
            )
          ],
        ),
        body: PageView.builder(
          itemBuilder: (BuildContext context, int index) {
            return _pages[index];
          },
          controller: _pageController,
          itemCount: _pages.length,
          onPageChanged: (int index) {
            setState(() {
              _currentIndex = index;
            });
          },
        ),
        bottomNavigationBar: botNavBar);
  }
}
